package team.ttorder.ttorder.service;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;

import team.ttorder.ttorder.dao.FoodMapper;
import team.ttorder.ttorder.dao.OrderDetailMapper;
import team.ttorder.ttorder.dao.OrderMapper;
import team.ttorder.ttorder.dao.TableMapper;
import team.ttorder.ttorder.javabean.PageBean;
import team.ttorder.ttorder.model.Food;
import team.ttorder.ttorder.model.Order;
import team.ttorder.ttorder.model.OrderDetail;
import team.ttorder.ttorder.model.Table;
@Service
public class OrderServiceImpl implements IOrderService {

	@Autowired
	private OrderMapper orderMapper;
	
	@Autowired
	private TableMapper tableMapper;
	
	@Autowired
	private OrderDetailMapper orderDetailMapper;
	
	@Autowired
	private FoodMapper foodMapper;
	
	@Override
	public Order getOrderById(Integer id) {
		Order order = orderMapper.selectByPrimaryKey(id);
		order.setTableName(tableMapper.selectByPrimaryKey(order.getTableId()).getTableName());
		return order;
	}

	@Override
	public void payComplete(Integer id) throws Exception {
		Order order = orderMapper.selectByPrimaryKey(id);
		order.setOrderStatus(1);
		orderMapper.updateByPrimaryKeySelective(order);
		Table table = tableMapper.selectByPrimaryKey(order.getTableId());
		table.setTablesStatus(0);
		table.setOrderDate(null);
		tableMapper.updateByPrimaryKey(table);
	}

	@Override
	public void deleteOrder(Integer id) {
		orderMapper.deleteByPrimaryKey(id);
		orderDetailMapper.deleteOrderDetailByOrderId(id);
	}

	@Override
	public List<OrderDetail> getOrderDetailByOrderId(Integer id) {
		List<OrderDetail> list = orderDetailMapper.getOrderDetailByOrderId(id);
		float totalPrice = 0;
		for(int i = 0; i < list.size(); i++) {
			Food food = foodMapper.selectByPrimaryKey(list.get(i).getFoodId());
			list.get(i).setFoodName(food.getFoodName());
			totalPrice += food.getPrice() * list.get(i).getFoodCount();
			list.get(i).setTotalPrice(orderMapper.selectByPrimaryKey(list.get(i).getOrderId()).getTotalPrice());
		}
		if(totalPrice == list.get(0).getTotalPrice()) {
			for(int i = 0; i < list.size(); i++) {
				list.get(i).setPrice(foodMapper.selectByPrimaryKey(list.get(i).getFoodId()).getPrice());
			}
		}else {
			for(int i = 0; i < list.size(); i++) {
				list.get(i).setPrice(foodMapper.selectByPrimaryKey(list.get(i).getFoodId()).getmPrice());
			}
		}
		return list;
	}

	@Override

	public void addOrder(Order order) {
		
		orderMapper.insert(order);
	}

	@Override
	public Order getId(int tableId) {
		
		return orderMapper.getId(tableId);
	}

	@Override
	public void addOrderDetail(OrderDetail orderDetail) {
		
		orderDetailMapper.insert(orderDetail);
	}

	public PageBean<Order> findOrders(int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		List<Order> list = orderMapper.selectAll();
		return new PageBean<>(list);

	}
	
}
